package io.jpress.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.PropKit;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QiniuUtils {
	private static Logger logger = LoggerFactory.getLogger(QiniuUtils.class);
	static String ACCESS_KEY = PropKit.get("qiniu_access_key");
	static String SECRET_KEY = PropKit.get("qiniu_secret_key");
	static String BUCKET = PropKit.get("qiniu_bucket");
	static String QINIU_IMAGE = PropKit.get("qiniu_image");

	static Configuration cfg = new Configuration(Zone.zone2());
	static UploadManager uploadManager = new UploadManager(cfg);

	public static String update(String filePath, String fileName) {
		try {
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
			String upToken = auth.uploadToken(BUCKET);
			Response response = uploadManager.put(filePath, fileName, upToken);
			logger.info(response.toString());
			return fileName;
		} catch (QiniuException ex) {
			Response r = ex.response;
			logger.error(r.toString());
		}
		return null;
	}
}
